package com.tuanzi.loan.business.repository.receipt.statistics.impl;

import com.tuanzi.loan.business.repository.receipt.statistics.custom.ReceiptBalanceOperationRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.view.receipt.statistics.ReceiptBalanceOperationRecordView;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptBalanceOperationRecord.LOAN_RECEIPT_BALANCE_OPERATION_RECORD;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptOrder.LOAN_RECEIPT_ORDER;

/**
 * @author lsx
 */
@Repository
public class ReceiptBalanceOperationRecordRepositoryImpl extends JOOQBaseRepositoryPlus implements ReceiptBalanceOperationRecordRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return LOAN_RECEIPT_BALANCE_OPERATION_RECORD;
    }

    @Override
    public ReceiptBalanceOperationRecordView findOperationRecordViewByUserId(String userId, Date startTime, Date endTime) {
        fields(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID, DSL.count().as("deliverCount"), DSL.sum(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.BALANCE).as("sumBalance"));
        eq(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID, userId);
        ge(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, startTime);
        le(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, endTime);
        Record record = searchOne();
        ReceiptBalanceOperationRecordView view = new ReceiptBalanceOperationRecordView();
        if (record.size() > 0) {
            view.setUserId(record.get(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID));
            view.setDeliveryCount(record.get("deliverCount", Integer.class));
            view.setConsumePrice(record.get("sumBalance", Float.class));
        }
        return view;
    }

    @Override
    public List<ReceiptBalanceOperationRecordView> findRecordResponseSourceFromByUserId(String userId, Date startTime, Date endTime) {
        fields(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID, LOAN_RECEIPT_ORDER.SOURCE_FROM, DSL.count().as("deliverCount"),
                DSL.sum(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.BALANCE).as("sumBalance"));
        leftJoin(LOAN_RECEIPT_ORDER, LOAN_RECEIPT_BALANCE_OPERATION_RECORD.RECEIPT_ORDER_ID.eq(LOAN_RECEIPT_ORDER.ID));
        eq(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID, userId);
        ge(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, startTime);
        le(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, endTime);
        groupBy(LOAN_RECEIPT_ORDER.SOURCE_FROM);
        return search(0,Integer.MAX_VALUE).map(record -> {
            ReceiptBalanceOperationRecordView response = new ReceiptBalanceOperationRecordView();
            response.setUserId(record.get(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.USER_ID));
            response.setConsumePrice(record.get("sumBalance", Float.class));
            response.setDeliveryCount(record.get("deliverCount", Integer.class));
            response.setSourceFrom(record.get(LOAN_RECEIPT_ORDER.SOURCE_FROM));
            return  response;
        });
    }

    @Override
    public Long findSettleAmountByProductIdAndSourceFrom(Long productId, Integer sourceFrom, Date startTime, Date endTime) {
        fields(DSL.sum(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.BALANCE).as("sumBalance"));
        leftJoin(LOAN_RECEIPT_ORDER, LOAN_RECEIPT_BALANCE_OPERATION_RECORD.RECEIPT_ORDER_ID.eq(LOAN_RECEIPT_ORDER.ID));
        ge(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, startTime);
        le(LOAN_RECEIPT_BALANCE_OPERATION_RECORD.OPERATION_TIME, endTime);
        eq(LOAN_RECEIPT_ORDER.PRODUCT_ID,productId);
        eq(LOAN_RECEIPT_ORDER.SOURCE_FROM,sourceFrom);
        return searchOne().into(Long.class);
    }
}
